package com.wisezone.workflow.core.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.wisezone.workflow.bean.WfActivity;
import com.wisezone.workflow.bean.WfLink;
import com.wisezone.workflow.bean.search.WfLinkSearch;
import com.wisezone.workflow.core.Activity;
import com.wisezone.workflow.core.Link;
import com.wisezone.workflow.core.ProcessDefinition;
import com.wisezone.workflow.exception.WfCoreException;

/**
 * 活动对象
 * @author tss 2009-09-05
 *
 */
public class ActivityImpl  extends BasePropertyImpl implements Activity{

	
	private ProcessDefinition processDefinition;
	
	private WfActivity wfActivity;
	
	
	/**
	 * 构造方法
	 * @param processDefinition
	 * @param wfActivity
	 */
	public ActivityImpl(ProcessDefinition processDefinition, WfActivity wfActivity) {
		
		super();
		this.processDefinition = processDefinition;
		this.wfActivity = wfActivity;
		this.setDao(processDefinition);
	}

	/**
	 * 获得下一活动
	 * @return
	 * @throws WfCoreException
	 */
	public Activity getNextActivity() throws WfCoreException{
		try {
			
			WfActivity nextWfActivity = this.getWfActivityDao().getNextActivity(wfActivity.getId());
			Activity nextActivity = new ActivityImpl(this.getProcessDefinition(),
					nextWfActivity);
			return nextActivity;
			
		} catch (WfCoreException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	/**
	 * 获得下一活动
	 * @param name 下一活动名称
	 * @return
	 * @throws WfCoreException
	 */
	public Activity getNextActivity(String name) throws WfCoreException{
		
		if(StringUtils.isEmpty(name)){
			throw new IllegalArgumentException("活动名称不能为空");
		}
		try {
			
			WfActivity nextWfActivity = this.getWfActivityDao().getNextActivity(wfActivity.getId(),name);
			Activity nextActivity = new ActivityImpl(this.getProcessDefinition(),
					nextWfActivity);
			return nextActivity;
			
		} catch (WfCoreException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	
	/**
	 * 获得活动的下一个连接集合
	 * @return
	 * @throws WfCoreException
	 */
	public List<Link> getLinks() throws WfCoreException {
		
		WfLinkSearch searchEntity = new WfLinkSearch();
		searchEntity.setPreActivityId(getModel().getId());
		List<WfLink> linkList = getWfLinkDao().listAll(searchEntity);
		List<Link> list = new ArrayList<Link>();
		for(Iterator<WfLink> it = linkList.iterator(); it.hasNext();){
			WfLink wfLink = it.next();
			Link entity = new LinkImpl(wfLink,processDefinition);
			entity.setModel(wfLink);
			list.add(entity);
		}
		return list;
	}
	

	public ProcessDefinition getProcessDefinition() {
		return processDefinition;
	}

	public void setProcessDefinition(ProcessDefinition processDefinition) {
		this.processDefinition = processDefinition;
	}

	public WfActivity getModel() {
		return wfActivity;
	}

	public void setModel(WfActivity wfActivity) {
		this.wfActivity = wfActivity;
	}


    
}
